//
//  KLNavTitleView.m
//  KL
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//
#define KLScrollViewSubviewsCount self.scrollview.subviews.count

#import "KLNavTitleView.h"

@interface KLNavTitleView () <UIScrollViewDelegate>
/** 用来装label */
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
/** 显示页数 */
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@end

@implementation KLNavTitleView

#pragma mark - 从xib中加载
+ (instancetype)navTitleView {
    return [[NSBundle mainBundle] loadNibNamed:@"KLNavTitleView" owner:nil options:nil][0];
}

#pragma mark - 初始化控件
- (void)awakeFromNib {
    // 设置scrollview的属性
    self.scrollview.showsHorizontalScrollIndicator = NO;
    self.scrollview.showsVerticalScrollIndicator = NO;
    self.scrollview.pagingEnabled = YES;
    self.scrollview.delegate = self;
    
    // 设置pagecontrol的属性
    [self.pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKeyPath:@"_pageImage"];
    [self.pageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKeyPath:@"_currentPageImage"];
    self.pageControl.userInteractionEnabled = NO;
    
    // 添加label
    [self addTitleLabel:@"随便逛逛"];
    [self addTitleLabel:@"精华"];
    [self addTitleLabel:@"有图有真相"];
    [self addTitleLabel:@"纯文"];
    
}

- (void)addTitleLabel:(NSString *)title {
    KLNavTitleLabel *label = [[KLNavTitleLabel alloc] init];
    label.font = [UIFont systemFontOfSize:13];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = title;
    [self.scrollview addSubview:label];
}

#pragma mark - 设置frame
- (void)layoutSubviews {
    [super layoutSubviews];
    // 遍历scrollview中得子控件
    NSInteger index = 0;
    CGFloat labelW = self.scrollview.w;
    CGFloat labelH = self.scrollview.h;
    for (UIView *subView in self.scrollview.subviews) {
            subView.frame = CGRectMake(index * labelW, 0, labelW, labelH);
            index ++;
    }
    
    // 设置scrollview的contentsize
    self.scrollview.contentSize = CGSizeMake(index * labelW, 0);
    self.pageControl.numberOfPages = self.scrollview.subviews.count;
}

#pragma mark - 重写偏移量的setter方法,让scrollview移动
- (void)setOffsetX:(CGFloat)offsetX {
    _offsetX = offsetX;
    // 这里不能用动画的形式偏移,因为那个方法是在动画结束的时候才会调用
    self.scrollview.contentOffset = CGPointMake(offsetX, 0);
    
    CGFloat offset = offsetX / self.w;

    // 最大偏移量
    CGFloat maxOffset = KLScrollViewSubviewsCount - 1;
    // 偏移量不能小于0或者大于最大的偏移量
    if (offset < 0 || offset > maxOffset) return;
    
    // 算出当前也就是左边的label的下标
    NSInteger leftIndex = (NSInteger)offsetX / self.w;
    
    // pagecontrol
    self.pageControl.currentPage = leftIndex;
    
    // 下一个要显示的label的下标
    NSInteger rightIndex = leftIndex + 1;
    // 如果是第五个时候直接返回
    if (rightIndex == KLScrollViewSubviewsCount) return;
    // 下一个的透明度
    CGFloat rightScale = offset - leftIndex;
    // 当前的透明度
    CGFloat leftSacle = 1 - rightScale;
    
    // 取出左边和右边的label
    KLNavTitleLabel *leftLabel = self.scrollview.subviews[leftIndex];
    if (rightIndex >= KLScrollViewSubviewsCount) {
        rightIndex = KLScrollViewSubviewsCount - 1;
    }
    KLNavTitleLabel *rightLabel = self.scrollview.subviews[rightIndex];
    
    leftLabel.scale = leftSacle;
    rightLabel.scale = rightScale;
    
}

@end
